<template>
  <div class="user-center">
    <el-form ref="ruleForm" style="width: 50%;" :model="ruleForm" :rules="rules" label-width="140px">
      <el-form-item label="旧密码" prop="oldPwd">
        <el-input v-model="ruleForm.oldPwd" placeholder="请输入旧密码" autocomplete="off" show-password />
      </el-form-item>
      <el-form-item label="新密码" prop="newPwd">
        <el-input v-model="ruleForm.newPwd" placeholder="请输入新密码" type="password" autocomplete="off" show-password />
      </el-form-item>
      <el-form-item label="确认密码" prop="reNewPwd">
        <el-input v-model="ruleForm.reNewPwd" placeholder="请再次输入新密码" type="password" autocomplete="off" show-password />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="ruleForm.email"
          placeholder="请输入邮箱"
          name="email"
        />
      </el-form-item>
      <el-button style="margin-left: 140px;margin-bottom: 20px;" type="primary" :loading="emailLoading" @click.native.prevent="getEmailCode">{{ emailStatusText }}</el-button>
      <el-form-item label="验证码" prop="code">
        <el-input v-model="ruleForm.code" placeholder="请输入获取的验证码" />
      </el-form-item>

      <el-form-item>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.newPwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        oldPwd: '',
        code: '',
        newPwd: '',
        email: '',
        reNewPwd: '',
        codeID: ''
      },
      emailLoading: false,
      emailStatusText: '获取验证码',
      codeTimer: null,
      emailTime: 60,
      rules: {
        oldPwd: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        reNewPwd: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    getEmailCode() {
      const reg = /^\w+((.\w+)|(-\w+))@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+).[A-Za-z0-9]+$/ // 正则表达式

      if (!reg.test(this.ruleForm.email)) {
        this.$message({
          message: '邮箱格式错误',
          type: 'warning'
        })
        return
      }
      this.emailLoading = true
      this.codeTimer = setInterval(() => {
        this.emailTime--
        if (this.emailTime === 0) {
          this.emailLoading = false
          this.emailStatusText = '获取验证码'
          this.emailTime = 60
          clearInterval(this.codeTimer)
        } else {
          this.emailStatusText = `${this.emailTime}秒后重试`
        }
      }, 1000)
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped>
.user-center {
  margin: 20px;
  margin-top: 40px;
}
</style>
